package com.check.operation.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.check.operation.R;
import com.check.operation.model.Inpatient;
import com.check.operation.model.MessagRe;
import com.check.operation.model.Operation;
import com.check.operation.model.RequestUse;
import com.check.operation.model.Result;
import com.check.operation.model.Sysdiclist;
import com.check.operation.model.User;
import com.check.operation.util.PlaySoundPool;
import com.szj.util.HttpUtil;
import com.szj.util.SysUtil;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ShAdapter extends BaseAdapter implements OnItemSelectedListener, OnClickListener {
	private LayoutInflater layoutInflater;
	private List<Operation> cws;
	private User user;
	private ArrayAdapter<Sysdiclist> sfdgdic;
	private Sysdiclist sfdgd;
	PlaySoundPool playSoundPool;
	private Context context;
	private String zyh;

	public ShAdapter(LayoutInflater layoutInflater, List<Sysdiclist> sysdiclists, User user) {
		super();
		this.layoutInflater = layoutInflater;
		this.user = user;
		this.context = this.layoutInflater.getContext();
		sfdgdic = new ArrayAdapter<Sysdiclist>(context, android.R.layout.simple_spinner_item);
		sfdgdic.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sfdgdic.addAll(sysdiclists);
		playSoundPool = new PlaySoundPool(layoutInflater.getContext());
		playSoundPool.loadSfx(R.raw.ok, 1);
		playSoundPool.loadSfx(R.raw.error, 2);
	}

	@Override
	public int getCount() {
		return cws.size();
	}

	@Override
	public Operation getItem(int position) {
		return cws.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		Operation operation = getItem(position);
		ViewHolder holder = null;

		if (view == null) {
			view = layoutInflater.inflate(R.layout.shxx, null);
			holder = new ViewHolder();
			holder.ssmc = (TextView) view.findViewById(R.id.ssmc);
			holder.sqzd = (TextView) view.findViewById(R.id.sqzd);
			holder.zdys = (TextView) view.findViewById(R.id.zdys);
			holder.sssq = (TextView) view.findViewById(R.id.sssq);
			holder.ssdg = (Spinner) view.findViewById(R.id.ssdg);
			holder.ssdg.setAdapter(sfdgdic);
			holder.ssdg.setOnItemSelectedListener(this);
			holder.hd = (Button) view.findViewById(R.id.hd);
			holder.hd.setOnClickListener(this);
			// holder.sstab=(TableLayout) view.findViewById(R.id.sstab);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		holder.hd.setTag(holder);
		holder.operation = operation;
		holder.ssmc.setText(operation.order_name);
		holder.sqzd.setText(operation.op_beforediag);
		holder.zdys.setText(operation.emp_name);
		holder.sssq.setText(operation.op_datetime);
		holder.position = position;
		return view;
	}

	public static class ViewHolder {
		TextView ssmc;
		TextView sqzd;
		TextView zdys;
		TextView sssq;
		Spinner ssdg;
		Button hd;
		TableLayout sstab;
		Operation operation;
		int position;
		
		public void success(Context context) {
			hd.setTextColor(context.getResources().getColor(R.color.绿色));
		}
		
		public void fail(Context context) {
			hd.setTextColor(context.getResources().getColor(R.color.红色));
		}
	}

	public List<Operation> getCws() {
		return cws;
	}

	public void setCws(List<Operation> cws) {
		this.cws = cws;
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		if (arg0.getId() == R.id.ssdg) {
			sfdgd = this.sfdgdic.getItem(arg2);
		}
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
	}

	@Override
	public void onClick(View v) {
		final ViewHolder viewHolder = (ViewHolder) v.getTag();
		countBb(viewHolder.operation, v, viewHolder.position);
	}

	private void countBb(Operation operation, final View v, int position) {
		int count = hasSpec(zyh);
		if (count > 0) {
			Bundle bundel = new Bundle();
			bundel.putString("surgicNo", operation.surgic_no);
			bundel.putString("processType", "f");
			bundel.putString("position", operation.op_position);
			bundel.putString("room", operation.op_room_id);
			bundel.putString("action", "process/process");
			bundel.putString("inpat", zyh);
			bundel.putString("order_name", operation.order_name);
			bundel.putString("op_position_name", operation.op_position_name);
			
			bundel.putInt("index", position);
			
			Intent intent = new Intent();
			intent.putExtra("data", bundel);
//			intent.setAction(InshActivity.ACTION);
			layoutInflater.getContext().sendBroadcast(intent);
		} else {
			Toast.makeText(context, "该病人无标本！", Toast.LENGTH_SHORT).show();
			sure(v);
		}
		/*
		 * DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd",
		 * Locale.getDefault()); String action =
		 * "specimen/list?zyh="+zyh+"&RoomId="+operation.op_room_id+"&Date="+dateFormat.
		 * format(new Date()); String rs = HttpUtil.get(action);
		 * MessagRe<List<Specimen>> re = JSONObject.parseObject(rs, new
		 * TypeReference<MessagRe<List<Specimen>>>(){}); int count = re.value.size();
		 * if("success".equals(re.message.code)){ new
		 * AlertDialog.Builder(this.context).setTitle("确认？")
		 * .setMessage("该病人当前有"+count+"个标本") .setPositiveButton("是", new
		 * DialogInterface.OnClickListener() {
		 * 
		 * @Override public void onClick(DialogInterface dialog, int which) { //
		 * sure(v); } }) .setNegativeButton("否", new DialogInterface.OnClickListener() {
		 * 
		 * @Override public void onClick(DialogInterface dialog, int which) {
		 * dialog.dismiss(); } }).show(); } else {
		 * 
		 * }
		 */
	}

	private int hasSpec(String inpat) {
		String url = "bbsy!hasSpecimen.action?inpat=" + inpat;
		String rs = HttpUtil.get("http://199.199.4.20:8085/rmyyblksqd/", url);
		if (SysUtil.isNotNUll(rs)) {
			Result<Integer> result = JSONObject.parseObject(rs, new TypeReference<Result<Integer>>() {
			});
			if ("success".equals(result.code) && result.value != null) {
				return result.value;
			}
		}
		return 0;
	}

	private void sure(View v) {
		final Button butt = (Button) v;
		butt.setTextColor(layoutInflater.getContext().getResources().getColor(R.color.紫色));

		String action = "process/process";
		final RequestUse requestUse = new RequestUse();

		final ViewHolder viewHolder = (ViewHolder) v.getTag();
		Operation operation = viewHolder.operation;

		requestUse.user = this.user;
		Map<String, Object> params1 = new HashMap<String, Object>();
		params1.put("surgicNo", operation.surgic_no);
		params1.put("processType", "f");
		params1.put("position", sfdgd.dic_code);
		params1.put("room", operation.op_room_id);
		params1.put("specimen", 2);
		requestUse.request = params1;
		String rs = HttpUtil.postJSON(action, JSONObject.toJSONString(requestUse));
		MessagRe<Inpatient> mereturn = JSONObject.parseObject(rs, new TypeReference<MessagRe<Inpatient>>() {
		});
		if ("success".equals(mereturn.message.code)) {
			playSoundPool.play(1, 0);
			Toast.makeText(layoutInflater.getContext(), mereturn.message.text, Toast.LENGTH_SHORT).show();
			butt.setTextColor(layoutInflater.getContext().getResources().getColor(R.color.绿色));
		} else {
			playSoundPool.play(2, 0);
			butt.setTextColor(layoutInflater.getContext().getResources().getColor(R.color.红色));
			Toast.makeText(layoutInflater.getContext(), mereturn.message.text, Toast.LENGTH_SHORT).show();
		}
	}

	public String getZyh() {
		return zyh;
	}

	public void setZyh(String zyh) {
		this.zyh = zyh;
	}

}
